home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ddj1190.arc / E_FLOYD.ARC / MAKEDICT.PAS < prev    next >
Pascal/Delphi Source File  |  1990-10-27  |  4KB  |  121 lines

  1. {$A+,B-,D-,E-,F+,I+,L-,N-,O-,R-,S-,V+}
  2. {$M 16384,0,655360}
  3. Program MakeDict;
  4. { Build an optimal dictionary from an input word list.  The input word list
  5.   should contain one word per line.  The output dictionary is saved to disk.
  6.   Run MAKEDICT with no parameters for brief documentation. }
  7. Uses Dict, Crt, Dos;
  8. Const
  9.   DefaultBits = 13;      { Default number of bits per word }
  10.  
  11. Var
  12.   Words : LongInt;       { Number of input words }
  13.   ExtraWords : LongInt;  { Number of extra words to allow in dictionary }
  14.   Bits : Byte;           { Number of bits to represent each word }
  15.   InFileName : PathStr;  { Input file name }
  16.   OutFileName : PathStr; { Output file name }
  17.   InBuf : Array[1..16384] Of Char; { I/O buffer for input }
  18.  
  19. Procedure InterpretParam;
  20. { Interpret input parameters }
  21. Var
  22.   i : Integer;
  23.   p : PathStr;
  24.   d : DirStr;
  25.   n : NameStr;
  26.   e : ExtStr;
  27. Begin
  28.   If ParamCount < 1 Then Begin
  29.     WriteLn('Run like this: ');
  30.     WriteLn;
  31.     WriteLn('  MAKEDICT <infile> [<bits>] [<extra>]');
  32.     WriteLn;
  33.     WriteLn('<infile> specifies the input file name, and optional <bits>');
  34.     WriteLn('specifies the number of bits to use for each word.  If <bits>');
  35.     WriteLn('is omitted, the program will use 13 bits per word.  Optional');
  36.     WriteLn('<extra> is the number of extra words to allow for in the');
  37.     WriteLn('dictionary.  The output file has the same name as the input');
  38.     WriteLn('file, but the extension will be ".DCT" (or ".DIC" if the input');
  39.     WriteLn('file extension is ".DCT").  Examples:');
  40.     WriteLn;
  41.     WriteLn('  MAKEDICT atob.txt 14');
  42.     WriteLn('  MAKEDICT ctod.txt');
  43.     WriteLn('  MAKEDICT user.lst 12 2000');
  44.     Halt(1);
  45.   End;
  46.   p := FExpand(ParamStr(1));
  47.   FSplit(p, d, n, e);
  48.   InFileName := p;
  49.   If e = 'DCT' Then e := 'DIC' Else e := 'DCT';
  50.   OutFileName := n + '.' + e;
  51.   If ParamCount > 1 Then Begin
  52.     Val(ParamStr(2), Bits, i);
  53.     If (i <> 0) Or (Bits < 1) Then Begin
  54.       WriteLn('Bits value is ', ParamStr(2), ', should be 1..255');
  55.       Halt(1);
  56.     End;
  57.   End Else Bits := DefaultBits;
  58.   If ParamCount > 2 Then Begin
  59.     Val(ParamStr(3), ExtraWords, i);
  60.     If (i <> 0) Or (ExtraWords < 0) Then Begin
  61.       WriteLn('Extra words value is ', ParamStr(2), ', should be numeric > 0');
  62.       Halt(1);
  63.     End;
  64.   End Else ExtraWords := 0;
  65. End;
  66.  
  67. Procedure CountWords;
  68. { Read the input file and count the words }
  69. Var
  70.   InFile : Text;
  71.   s : String;
  72. Begin
  73.   Assign(InFile, InFileName);
  74.   SetTextBuf(InFile, InBuf);
  75.   Reset(InFile);
  76.   Words := 0;
  77.   Repeat
  78.     ReadLn(InFile, s);
  79.     If s <> '' Then Inc(Words);
  80.   Until Eof(InFile);
  81.   Close(InFile);
  82. End;
  83.  
  84. Procedure BuildDictionary;
  85. { Read the input file and build the dictionary }
  86. Var
  87.   Count : LongInt;
  88.   i : Word;
  89.   d : Dictionary;
  90.   InFile : Text;
  91.   s : String;
  92. Begin
  93.   Assign(InFile, InFileName);
  94.   SetTextBuf(InFile, InBuf);
  95.   Reset(InFile);
  96.   Count := 0;
  97.   d.Init(Words + ExtraWords, Bits);
  98.   WriteLn('Reading ', InFileName, ' for ', Words, ' words, ', Bits, ' bits');
  99.   Repeat
  100.     ReadLn(InFile, s);
  101.     Inc(Count);
  102.     If (Count Mod 1000) = 0 Then Write(^M, Count);
  103.     If d.InsertString(s) Then
  104.       WriteLn(^M, Count, ' ', s, ' appears to be already in the dictionary.');
  105.   Until Eof(InFile);
  106.   Close(InFile);
  107.   WriteLn(^M, Count, ' words read ', d.DictCount,
  108.     ' words inserted in dictionary');
  109.   WriteLn('Estimated error chance: 1/', 1/d.EstError:1:0);
  110.   WriteLn('Actual error chance: 1/', 1/d.ActError:1:0);
  111.   WriteLn('Saving ', OutFileName, ', ', d.DictionarySize, ' bytes');
  112.   d.SaveDictionary(OutFileName);
  113.   d.Done;
  114. End;
  115.  
  116. Begin
  117.   InterpretParam;
  118.   CountWords;
  119.   BuildDictionary;
  120. End.
  121.